import { defineStore } from 'pinia'
import { store } from '../index'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
const { wsCache } = useCache()

export const useAnnotationStore = defineStore('annotation', {
  state: () => ({
    currentGalleryId: 0
  }),

  getters: {
    getId() {
      return wsCache.get(CACHE_KEY.CurGalleryId) ?? this.currentGalleryId
    }
  },

  actions: {
    setCurrentGalleryId(galleryId: number) {
      const storageCurrentGalleryId = wsCache.get(CACHE_KEY.CurGalleryId)
      if (!storageCurrentGalleryId) {
        wsCache.set(CACHE_KEY.CurGalleryId, galleryId)
        this.currentGalleryId = galleryId
      } else {
        this.currentGalleryId = storageCurrentGalleryId
      }
    }
  },
  persist: {
    key: 'annotation',
    storage: localStorage
  }
})

export const useAnnotationStoreWithOut = () => {
  return useAnnotationStore(store)
}
